﻿
using System;
using System.Linq;
using System.Collections.Generic;

namespace Demo.Test.StackQueue
{
    /// <summary>
    /// 抽象栈
    /// </summary>
    public abstract class AbsStack<T> : IStack<T> where T : class
    {
        private IList<Node<T>> data;
        private Int32 count;

        /// <summary>
        /// 栈容器数据集合
        /// </summary>
        public IList<Node<T>> Data 
        {
            get
            {
                return this.data;
            }
            set
            {
                data = value;
            }
        }

        /// <summary>
        /// 栈容器数据数量
        /// </summary>
        public Int32 Count
        {
            get
            {
                return this.count;
            }
            set
            {
                this.count = value;
            }
        }

        public AbsStack()
        {
            this.data = new List<Node<T>>();
        }

        public AbsStack(Node<T> node):base()
        {
            In(node);
        }

        /// <summary>
        /// 出栈
        /// </summary>
        /// <param name="node">节点 实例</param>
        public Node<T> Out()
        {
            var obj1 = this.data.Where(d => d.Next == null).FirstOrDefault();
            this.data.Remove(obj1);
            var obj2 = this.data.Where(d => d.Next != null).OrderByDescending(d=>d.Next).FirstOrDefault();
            obj2.Next = null;
            return obj1;
        }

        /// <summary>
        /// 入栈
        /// </summary>
        /// <param name="node">节点实例</param>
        public void In(Node<T> node)
        {
            if(data.Count == 0 )
            {
                node.Pre = null;
            }
            else
            {
                var obj = this.data.Where(d => d.Next == null ).FirstOrDefault();
                obj.Next = node;
                node.Pre = obj;
            }
            node.Next = null;
            data.Add(node);
        }

    }

    public class Stack : AbsStack<Entity>
    { 
        
    }

}
